#include<iostream>

using namespace std;

#define ture 1
#define false 0

template<class T>
struct listNode{
    T data;
    listNode<T> *pre;
    listNode<T> *next;

    listNode(listNode<T> *ptr = NULL) {next = ptr;}
};
template<class T>
class list{
    private :
        listNode<T>    *head;
        listNode<T>    *tail;
        unsigned int   len;
    public:
         list() ;
        ~list() ;
        void clear(){};
        int length() ;
        void sort();
        void reverse(){};
        void Print()  const;
        void erase(listNode<T> *node);
        void push_front(T value);
        void push_back(T value);
        void list_insert(listNode<T> *pos_node, T* value, bool back);
};


template<class T>
void list<T>::Print() const
{
    int count = 0;
    listNode<T> *p = head;
    while(NULL != p)
    {
        cout << p->data << " ";
        p = p->next;
        if(++count %10 == 0)
            cout << endl;
    }
}


template<class T>
list<T>::list(void)
{
    head = new listNode<T>;
    head = tail;
    len = 0;
}

template<class T>
int list<T>::length()
{
    return head->len;
}


template<class T>
void list<T>::push_front(T value)
{
    listNode<T> *node = new listNode<T>;
    
    node->data = value;
    if(len == 0)
    {
        head = tail = node;
        node->pre  = node->next = NULL;
    }
    else
    {
        node->pre  = NULL;
        node->next = head;
        head->pre = node;
        head = node;
    }
    len++;
}

template<class T>
void list<T>::sort()
{
    ;   
}

template<class T>   
void list<T>::push_back(T value)
{
    listNode<T> *node = new listNode<T>;

    node->data = value;
    if(len == 0)
    {
        head = tail = node;
        node->pre = node->next = NULL;
    }
    else
    {
        node->next = NULL;
        node->pre = tail;
        tail->next = node;
        tail = node;
    }
    len++;
}

template<class T>
void list<T>::list_insert(listNode<T> *pos_node, T* value, bool back)
{
    listNode<T> *node = new listNode<T>;
    
    node->data = value;
    if(back)
    {
        node->pre = pos_node;
        node->next = pos_node->next;
        if(tail = pos_node)
        {
            tail = node;
        }
    }
    else
    {
        node->next = pos_node;
        node->pre  = pos_node->pre;
        if(head == pos_node)
        {
            head = node;
        }
    }
    if(node->pre != NULL)
    {
        node->pre->next = node;
    }
    if(node->next != NULL)
    {
        node->next->pre = node;
    }
    len++;
}

template<class T>
void list<T>::erase(listNode<T> *node)
{
    if(node->pre)
    {
        node->pre->next = node->next;
    }
    else
    {
        head = node->next;
    }

    if(node->next)
    {
        node->next->pre = node->pre;
    }
    else
    {
        tail = node->pre;
    }

    delete(node);
    len--;
}

template<class T>
    
list<T>::~list()
{
    unsigned long len;
    listNode<T> *current, *next;
    current = head;
 
    while(len--) 
    {
        next = current->next;
        delete(current);
        current = next;
    }
}

int main()
{
    
    list<int> *l1 = new list<int>;
    int a = 3;
    l1->push_front(a);
    l1->push_front(a);
    l1->push_back(2);

    l1->Print();
    return 0;
}
